import 'dart:io';

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:lcapp/api/interface/login_api.dart';
import 'package:lcapp/api/request/response_data.dart';
import 'package:lcapp/basic/user_info.dart';
import 'package:lcapp/config/const_config.dart';
import 'package:lcapp/model/event/event_bus.dart';
import 'package:lcapp/model/toast/toast.dart';
import 'package:lcapp/page/drawer_page.dart';
import 'package:lcapp/routes/route.dart';
import 'package:webview_flutter/webview_flutter.dart';

class CancleAccountPage extends StatefulWidget {
  final Map arguments;
  const CancleAccountPage({Key? key, required this.arguments}):super(key: key);

  @override
  State<CancleAccountPage> createState() => _CancleAccountPageState();
}

class _CancleAccountPageState extends State<CancleAccountPage> {

  late WebViewController _controller;
  bool isCheck = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    // if (Platform.isAndroid) {
    //   WebView.platform = SurfaceAndroidWebView();
    // }
    print('url====='+widget.arguments["url"]);
    _controller = WebViewController()
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..setNavigationDelegate(
        NavigationDelegate(
          onProgress: (int progress) {
            // Update loading bar.
          },
          onPageStarted: (String url) {},
          onPageFinished: (String url) {},
          onHttpError: (HttpResponseError error) {},
          onWebResourceError: (WebResourceError error) {},
          onNavigationRequest: (NavigationRequest request) {
            if (request.url.startsWith('https://www.youtube.com/')) {
              return NavigationDecision.prevent;
            }
            return NavigationDecision.navigate;
          },
        ),
      )
      ..loadRequest(Uri.parse(widget.arguments["url"]));

  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 1.sw,
      height: 1.sh,
      child: Center(
        child: Container(
          margin: EdgeInsets.symmetric(horizontal: 40.w),
          padding: EdgeInsets.only(bottom: 30.w),
          height: 0.7.sh,
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(20.w)
          ),
          child: Column(
            children: [
              Expanded(child: ClipRRect(
                child: WebViewWidget(
                  controller: _controller,
                  // javascriptMode: JavascriptMode.unrestricted,
                  // initialUrl: widget.arguments["url"],
                  // onWebViewCreated: (controller) {
                  //   _controller = controller;
                  //   _controller.clearCache();
                  //   showLoading(maskType: EasyLoadingMaskType.none);
                  // },
                  // javascriptChannels: const <JavascriptChannel>{
                  // },
                  // onPageFinished: (url) {
                  //   hideLoading();
                  // },
                  // onWebResourceError: (error) {
                  //   hideLoading();
                  // },
                  // onPageStarted: (str) {},
                ),
                borderRadius: BorderRadius.circular(20.w),
              ),),
              Container(
                margin: EdgeInsets.only(top: 15.w),
                child: Row(
                  mainAxisSize: MainAxisSize.min,
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    GestureDetector(
                      onTap: () {
                        setState(() {
                          isCheck = !isCheck;
                        });
                      },
                      child: Container(
                        color: Colors.transparent,
                        width: 50.w,
                        padding: EdgeInsets.only(top: 20.w,bottom: 20.w),
                        // margin: EdgeInsets.only(right: 8.w),
                        child: Icon(
                          isCheck
                              ? Icons.check_circle
                              : Icons.panorama_fish_eye,
                          size: 30.w,
                          color:
                          isCheck ? Color(0xffFA6E28) : Colors.grey,
                        ),
                      ),
                    ),
                    Expanded(child:  RichText(
                      maxLines: 2,
                      softWrap: true,
                      text: TextSpan(
                        text: '我已阅读并同意以下协议',
                        style: TextStyle(
                          fontSize: 24.w,
                          color: const Color(0xff999999),
                        ),
                        children: <TextSpan>[
                          TextSpan(
                            text: '《注销协议》',
                            style:  TextStyle(
                              color: const Color(0xff224D79),
                              fontSize: 24.w,
                            ),
                            recognizer: TapGestureRecognizer()
                              ..onTap = () {
                                // Navigator.of(ASRRoute.navigatorKey.currentContext).pushNamed('H5Page',arguments: {
                                //   'url':widget.arguments['url'],
                                //   'title':'《用户协议》'
                                // });
                              },
                          ),
                        ],
                      ),
                      textAlign: TextAlign.left,
                    ),)
                  ],
                ),
              ),
             Row(
               mainAxisAlignment: MainAxisAlignment.center,
               children: [
                 Container(
                   width: 300.w,
                   height: 80.w,
                   alignment: Alignment.center,
                   decoration: BoxDecoration(
                       border: Border.all(color: Color(0xffeeeeee),width: 1),
                       borderRadius: BorderRadius.circular(100)
                   ),
                   child: GestureDetector(
                     onTap: ()async{
                       if(!isCheck){
                         showToast('请先同意注销协议');
                         return;
                       }
                       ResponseData res = await LoginAPI.cancleAccount();
                       if(res.code==200){
                         clearUserInfo();
                         eventBus.fire(HomeToIndex(index: 0));
                         Navigator.pushAndRemoveUntil(context, PageRouteBuilder(
                             settings: RouteSettings(name: "index"),
                             pageBuilder:
                                 (BuildContext context, Animation animation,
                                 Animation secondaryAnimation) {
                               return DrawerPage();
                             }), (route) => false);
                       }
                     },
                     child: Text('确认注销',style: TextStyle(
                         color: Color(0xff666666)
                     ),),
                   ),
                 ),
                 SizedBox(width: 30.w,),
                 Container(
                   width: 300.w,
                   height: 80.w,
                   alignment: Alignment.center,
                   decoration: BoxDecoration(
                       color: defaultBarColor,
                       borderRadius: BorderRadius.circular(100)
                   ),
                   child: GestureDetector(
                     onTap: (){
                       SmartDialog.dismiss();
                     },
                     child: Text('取消',style: TextStyle(
                         color: Colors.white
                     ),),
                   ),
                 )
               ],
             )
            ],
          ),
        ),
      ),
    );
  }

  @override
  void deactivate() {
    // TODO: implement deactivate
    super.deactivate();
    hideLoading();
  }
}
